package com.zzyl.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.ServeManagementMapper;
import com.zzyl.service.ServeManagementService;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.NursingProjectVo;
import com.zzyl.vo.PageNursingVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;


/**
 * @author zgl
 * @created 2024-07-08 15:19:30
 * @description
 */
@Slf4j
@Service
public class ServeManagementImpl implements ServeManagementService {
    private final ServeManagementMapper serveManagementMapper;

    public ServeManagementImpl(ServeManagementMapper serveManagementMapper) {
        this.serveManagementMapper = serveManagementMapper;
    }

    @Override
    public List<NursingProjectVo> SelectNursingProject() {
        return serveManagementMapper.SelectNursingProject();
    }

    /**
     * 删除护理项目
     * @param id 编号
     */
    @Override
    public void deleteNuringProject(Long id) {
        //判断有无关联护理计划
        NursingProject byPlanId = serveManagementMapper.selectByPlanId(id);
        if(Objects.nonNull(byPlanId)) {
            throw new BaseException(BasicEnum.DELETE_ERROR);
        }
        serveManagementMapper.deleteNuringProject(id);
    }

    /**
     * 启用禁用状态更新
     * @param id 编号
     * @param status 状态
     * @return  更新条数
     */
    @Override
    public void updateNuringProjectStatus(Long id, Integer status) {
        NursingProject nursingProject = serveManagementMapper.selectByPlanId(id);
        if (Objects.nonNull(nursingProject)) {
            throw new BaseException(BasicEnum.PLAN_ERROR);
        }
        serveManagementMapper.updateNuringProjectStatus(id,status);
    }

    /**
     * 更新项目
     * @param dto 参数
     * @return 更新条数
     */
    @Override
    public int updateNuringProject(NursingDto dto) {
        //1.验证空参数
        if (ObjectUtil.isEmpty(dto.getName())||
                ObjectUtil.isEmpty(dto.getPrice())||
                ObjectUtil.isEmpty(dto.getUnit())||
                ObjectUtil.isEmpty(dto.getOrderNo())||
                ObjectUtil.isEmpty(dto.getStatus())||
                ObjectUtil.isEmpty(dto.getImage())||
                ObjectUtil.isEmpty(dto.getNursingRequirement())) {

            throw new BaseException(BasicEnum.PARAM_ERROR);
        }
        //2.修改创建时间
        NursingProject nursingProject = new NursingProject();
        nursingProject.setUpdateTime(LocalDateTime.now());
        //2.查询有没有和护理计划绑定
        NursingProject byPlanId = serveManagementMapper.selectByPlanId(dto.getId());
        if (Objects.nonNull(byPlanId)) {
            throw new BaseException(BasicEnum.PLAN_ERROR);
        }
        //3.进行名字是否重复判断
        NursingProject nursingProject1 = serveManagementMapper.selectByName(dto.getName());
        if (Objects.nonNull(nursingProject1)) {
            throw new BaseException(BasicEnum.DUPLICATE_NAMES);
        }
        //4.复制
        BeanUtils.copyProperties(dto,nursingProject);
        //5.修改
        return  serveManagementMapper.updateNuringProject(nursingProject);
    }

    /**
     * 根据id查询
     * @param id 编号
     */
    @Override
    public NursingProjectVo selectById(Long id) {
        return serveManagementMapper.selectById(id);
    }

    @Override
    public PageResponse<PageNursingVO> PageNursing(String name, Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        Page<PageNursingVO> pageNursingVOS = serveManagementMapper.PageNursing(name,status);
        return PageResponse.of(pageNursingVOS,PageNursingVO.class);
    }

    @Override
    public int insertNuringProject(NursingDto dto) {
        // 1. 验证参数
        if (ObjectUtil.isEmpty(dto.getName())||
            ObjectUtil.isEmpty(dto.getPrice())||
            ObjectUtil.isEmpty(dto.getUnit())||
            ObjectUtil.isEmpty(dto.getOrderNo())||
            ObjectUtil.isEmpty(dto.getStatus())||
            ObjectUtil.isEmpty(dto.getImage())||
            ObjectUtil.isEmpty(dto.getNursingRequirement())) {

            throw new BaseException(BasicEnum.PARAM_ERROR);
        }
        //写入更新人
        NursingProject np = new NursingProject();
        np.setUpdateBy(1L);
        //复制
        BeanUtils.copyProperties(dto,np);
        // 2. 查找有没有相同名称
        NursingProject name = serveManagementMapper.selectByName(dto.getName());
        if (Objects.nonNull(name)){
            throw new BaseException(BasicEnum.DUPLICATE_PROJECTS);
        }
        //再进行添加
        return serveManagementMapper.insertNuringProject(np);
    }
}
